package alibaba.testutils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Arrays;

import junit.framework.Assert;

import alibaba.client.utils.Utils;
import alibaba.common.utils.Base64;

public class StringTestUtils {

    public static void assertOneLineContains(String output, String filename, int[] snapshots, byte[] signature) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader(output));
        int count = 0;
        String line = null;
        while ((line = reader.readLine()) != null) {
            String[] split = line.split(",");
            if (split[0].trim().equals(Utils.convertToUnixPath(filename)) && 
                split[2].trim().equals(Base64.encodeBytes(signature, Base64.URL_SAFE)) &&
                Arrays.equals(sort(snapshots), sort(createSnapshotsArray(split[1])))) {
                count += 1;
            }
        }
        
        Assert.assertEquals(1, count);
    }

    private static int[] createSnapshotsArray(String string) {
        String[] rawInts = string.trim().split(" ");
        int[] result = new int[rawInts.length];
        for (int i=0; i<result.length; i++) {
            result[i] = Integer.parseInt(rawInts[i].trim());
        }
        return result;
    }
    
    private static int[] sort(int[] array) {
        Arrays.sort(array);
        return array;
    }
    
}
